package qst.com.bacK.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qst.com.bacK.exception.ExamException;
import qst.com.bacK.mapper.AssessmentsMapper;
import qst.com.bacK.mapper.ExamMapper;
import qst.com.bacK.mapper.ExamQuestionMapper;
import qst.com.bacK.mapper.QuestionMapper;
import qst.com.bacK.pojo.entity.AssessmentType;
import qst.com.bacK.pojo.entity.Exam;
import qst.com.bacK.pojo.entity.ExamQuestion;
import qst.com.bacK.service.AssessmentService;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:   msh
 * Date:     2024/10/11 上午12:14
 * Description:
 */

@Service
public class AssessmentServiceImpl implements AssessmentService {
    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private ExamQuestionMapper examQuestionMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private AssessmentsMapper assessmentsMapper;

    @Override
    public void end(Exam exam) {
        try {
            // 设定交卷时间
            exam.setEndTime(new Timestamp(System.currentTimeMillis()));
            Map<Integer, Integer> map = new HashMap<>();
            map.put(1, 0);
            map.put(2, 0);
            map.put(3, 0);
            map.put(4, 0);
            // 判卷
            for (ExamQuestion eq : exam.getExamQuestions()) {
                // 判断该题学生答题是否正确
                eq.setRight(eq.getQuestion().isAnswerRight(eq.getAnswer()));
                if (eq.isRight()) {
                    int a = map.get(eq.getQuestion().getType()) + 1;
                    map.put(eq.getQuestion().getType(), a);
                } else {
                    int a = map.get(eq.getQuestion().getType()) - 1;
                    map.put(eq.getQuestion().getType(), a);
                }
            }
            StringBuffer stf = new StringBuffer();
            for (int i : map.keySet()) {
                if (map.get(i) > 0) {
                    if (i == 1) {
                        stf.append("J");
                    } else if (i == 2) {
                        stf.append("T");
                    } else if (i == 3) {
                        stf.append("S");
                    } else {
                        stf.append("E");
                    }
                } else {
                    if (i == 1) {
                        stf.append("P");
                    } else if (i == 2) {
                        stf.append("F");
                    } else if (i == 3) {
                        stf.append("N");
                    } else {
                        stf.append("I");
                    }
                }
            }
            stf.reverse();
            exam.setResult(stf.toString());
            // 保存考试
            if (exam.getId() != null) {
                examMapper.updateExam(exam);
            } else {
                examMapper.saveExam(exam);
            }
            // 保存学生的答题，备查
            examQuestionMapper.deleteByExam(exam.getId());
            for (ExamQuestion eq : exam.getExamQuestions()) {
                eq.setExamId(exam.getId());
                examQuestionMapper.save(eq, Arrays.toString(eq.getAnswer()));
            }
        } catch (SQLException e) {
            throw new ExamException("数据库访问出错，不能开始考试", e);
        }
    }

    @Override
    public void deleteAssessment(int id) {
        if (questionMapper.findCountByAssessment(id) > 0) {
            throw new ExamException("本考核类型已有试题，不能删除");
        }
        assessmentsMapper.deleteById(id);
    }

    @Override
    public AssessmentType findAssessmentById(int id) {
        return assessmentsMapper.findById(id);
    }

    @Override
    public List<AssessmentType> findAllAssessment() {
        return assessmentsMapper.findAll();
    }

    @Override
    public void saveAssessment(AssessmentType sj) {
        AssessmentType temp = assessmentsMapper.findByTitle(sj.getTitle());
        if (temp != null) {
            throw new ExamException("考核类型已存在");
        }
        assessmentsMapper.insert(sj);
    }

    @Override
    public void updateAssessment(AssessmentType sj) {
        AssessmentType temp = assessmentsMapper.findByTitle(sj.getTitle());
//        if (temp != null && temp.getId().equals(sj.getId())) {
//            throw new ExamException("考核类型重复");
//        }
        assessmentsMapper.update(sj);
    }

    @Override
    public AssessmentType findByTitle(String title) {
        return assessmentsMapper.findByTitle(title);
    }
}
